Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Variables

Variables Intro

Variables in Python

A variable in Python is a reserved memory location to store values. In other words, a variable in a Python program gives data to the computer for processing.
Basic example of variables in Python x = 5 y = "Hello, World!" print(x) print(y)

Output

5 Hello, World!
In the code above, x and y are variables. x stores an integer value of 5, and y stores a string value of ""Hello, World!"".

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _). Variable names are case-sensitive (age, Age and AGE are three different variables).

Assigning Values to Variables

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable.
Assigning values to variables counter = 100 # An integer assignment miles = 1000.0 # A floating point name = ""John"" # A string
Here, 100, 1000.0 and ""John"" are the values assigned to counter, miles, and name variables, respectively.

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously.
Assigning multiple values a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables.
Assigning multiple objects to multiple variables a, b, c = 1, 2, ""john""
Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value ""john"" is assigned to the variable c.

  📌TAGS

★python ★ variables

Tutorials